package com.hjy.scheduled.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hjy.scheduled.config.service.RedisReadWriteService;
import com.hjy.scheduled.mysql.repository.quartzRepository;
import com.hjy.scheduled.task.scheduler.QuartzScheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.hjy.scheduled.mysql.entity.quartz;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling
@Component
public class myTask {

    @Autowired
    private QuartzScheduler myScheduler;

    @Autowired
    private quartzRepository quartz;

    @Scheduled(cron = "*/5 * * * * ?")
    public void oneTask() {
        List<quartz> list2 = quartz.findByType(Short.parseShort("0"));//未执行

        for (quartz m: list2) {
            System.out.println("定时任务启动。。。。。。");
            try{
                myScheduler.startJob(m.getCron(),m.getName(),m.getId().toString(), "com.hjy.scheduled.task.job.taskJob");
                m.setType(Short.parseShort("1"));//正在执行
            }catch (Exception e){
                m.setType(Short.parseShort("2"));//发生错误
                e.printStackTrace();
            }
        }
        quartz.save(list2);
    }
}
